package com.example.meituan.until;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Environment;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.target.CustomTarget;
import com.bumptech.glide.request.transition.Transition;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.UUID;

public class FileImgUntil {

    private static final String PICTURES_DIR = Environment.DIRECTORY_PICTURES;

    /**
     * 生成一个唯一的图片文件名（不包含路径）
     */
    public static String generateUniqueFileName() {
        return UUID.randomUUID().toString().replace("-", "") + ".png";
    }

    /**
     * 将图片从 Uri 保存到指定路径并返回完整的文件路径
     */
    public static String saveImageAndGetPath(@NonNull Uri uri, @NonNull Context context) {
        // 检查外部存储是否可用
        if (!Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
            throw new IllegalStateException("External storage is not mounted.");
        }

        // 生成唯一文件名
        String fileName = generateUniqueFileName();
        // 创建完整路径
        File pictureDir = Environment.getExternalStoragePublicDirectory(PICTURES_DIR);
        if (!pictureDir.exists() && !pictureDir.mkdirs()) {
            throw new IllegalStateException("Failed to create directory.");
        }
        File file = new File(pictureDir, fileName);

        try (FileOutputStream fos = new FileOutputStream(file)) {
            Glide.with(context)
                    .asBitmap()
                    .load(uri)
                    .into(new CustomTarget<Bitmap>() {
                        @Override
                        public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) {
                            resource.compress(Bitmap.CompressFormat.PNG, 100, fos);
                        }

                        @Override
                        public void onLoadCleared(@Nullable Drawable placeholder) { /* Do nothing */ }
                    });
            fos.flush();
        } catch (IOException e) {
            e.printStackTrace();
            return null; // 或者抛出异常
        }

        return file.getAbsolutePath();
    }
    public static void saveImageBitmapToFile(Uri url, Context context,String path) {
        CustomTarget<Bitmap> target=new CustomTarget<Bitmap>() {
            @Override
            public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) {
                //实现一个保存图片
                File flie=new File(path);
                try{
                    FileOutputStream fos=new FileOutputStream(flie);
                    resource.compress(Bitmap.CompressFormat.PNG,100,fos);
                    fos.flush();
                    fos.close();
                }catch (IOException e){
                    e.printStackTrace();
                }

            }

            @Override
            public void onLoadCleared(@Nullable Drawable placeholder) {

            }
        };
        Glide.with(context)
                .asBitmap()
                .load(url)
                .into(target);
    }
    //获取路径
    public static String getPath(Uri uri){
        return Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).getAbsolutePath();
    }

    public  static String getImgPath()
    {
        //图片名字
        String pngName="/"+UUID.randomUUID().toString().replace("-","")+".png";
        return Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).getAbsolutePath()+pngName;
    }
}
